package org.penguin.study.bk.config

import org.springframework.boot.web.servlet.ServletComponentScan
import org.springframework.context.MessageSource
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration
import org.springframework.context.support.MessageSourceResourceBundle
import org.springframework.context.support.ResourceBundleMessageSource
import org.springframework.http.HttpMethod
import org.springframework.http.converter.HttpMessageConverter
import org.springframework.http.converter.StringHttpMessageConverter
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor
import org.springframework.web.servlet.LocaleResolver
import org.springframework.web.servlet.config.annotation.CorsRegistry
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor
import org.springframework.web.servlet.i18n.SessionLocaleResolver
import java.nio.charset.Charset
import java.util.*

//@EnableWebMvc
@Configuration
@ComponentScan("org.penguin.study.bk")
@ServletComponentScan("org.penguin.study.bk.web")
class ClientWebConfig : WebMvcConfigurer {

    override fun addCorsMappings(registry: CorsRegistry) {
        registry.addMapping("/**")
            .allowedOrigins("127.0.0.1")
            .allowCredentials(true)
            .allowedMethods(HttpMethod.GET.name, HttpMethod.POST.name, HttpMethod.DELETE.name, HttpMethod.PUT.name)
            .maxAge(3600)
    }

    @Bean
    fun methodValidationPostProcessor(): MethodValidationPostProcessor {
        return MethodValidationPostProcessor()
    }

    /**
     * 解决handleConstraintViolation乱码的问题
     */
    @Bean
    fun responseBodyConverter() = StringHttpMessageConverter(Charset.forName("UTF-8"))
    override fun configureMessageConverters(converters: MutableList<HttpMessageConverter<*>>) {
        super.configureMessageConverters(converters)
        converters.add(responseBodyConverter())
    }

//    /**
//     * 配置内容裁决的一些参数,
//     *
//     * 从spring5.2.4开始，ContentNegotiationConfigurer.favorPathExtension(false)是默认值，不用再配置了
//     */
//    override fun configureContentNegotiation(configurer: ContentNegotiationConfigurer) {
//        super.configureContentNegotiation(configurer)
//    }

    @Bean
    fun messageSource(): MessageSource? {
        val ms = ResourceBundleMessageSource()
        ms.setBasenames("messages")
        return ms
    }

    @Bean
    fun getBeanResourceBundle(): ResourceBundle? {
        val locale = Locale.getDefault()
        return MessageSourceResourceBundle(messageSource()!!, locale)
    }

    @Bean
    fun localeResolver(): LocaleResolver {
        var sessionResolver = SessionLocaleResolver()
        sessionResolver.setDefaultLocale(Locale.CHINA)
        return sessionResolver

//        var cookieResolver = CookieLocaleResolver()
//        cookieResolver.setDefaultLocale(Locale.CHINA)
//        cookieResolver.cookieName = "lang"
//        cookieResolver.cookieMaxAge = 3600 *24
//        return cookieResolver

//        // 默认的解析器
//        return AcceptHeaderLocaleResolver()
    }

    @Bean
    fun localeChangeInterceptor(): LocaleChangeInterceptor {
        val interceptor = LocaleChangeInterceptor()
        interceptor.paramName = "location"
        return interceptor
    }

    override fun addInterceptors(registry: InterceptorRegistry) {
        registry.addInterceptor(localeChangeInterceptor())
    }
}